package com.chancingpack.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.chancingpack.bean.WxBookBean;
import com.chancingpack.dal.model.WxBook;
import com.chancingpack.service.WxBookService;
import com.chancingpack.util.CommonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;

import java.text.SimpleDateFormat;
import java.util.*;


/**
 * Created by Administrator on 2017/7/28.
 */
@Controller
@RequestMapping("/myBook")
public class MyBookController {

    @Autowired
    WxBookService WxBookService;
/**
 * 我的购物车
 * */
    @RequestMapping("/bookList")
    public ModelAndView returnBookView(HttpServletRequest request){
        ModelAndView view = new ModelAndView("myproduct/myBook");
        String custId =request.getSession().getAttribute("custId").toString();
        Map<String,Object> map = new HashMap<String, Object>();
        map.put("custId",custId);
        List<Map<String,Object>> bookList= WxBookService.queryByCustId(map);
        List<Map<String,Object>> CustInfomap = WxBookService.querysgbasCustInfo(custId);
        if(bookList.size()==0){
            view.addObject("cup",false);
            return view;
        }
        for (int i =0 ;i<bookList.size();i++){
            //获取相应产品y压缩图片Url,通过archId将它放入map中
            String compressUrl = request.getContextPath() + "/resources/upload/compress/" + bookList.get(i).get("arch_id")+".jpg";
            bookList.get(i).put("compressImage",compressUrl);
            //获取相应产品原图片Url,通过archId将它放入map中
            String originalUrl = request.getContextPath() + "/resources/upload/original/" +bookList.get(i).get("arch_id") +".jpg";
            bookList.get(i).put("originalImage",originalUrl);
            bookList.get(i).put("cust_addra",CustInfomap.get(0).get("cust_addra"));
            Date date=new Date();//取时间
            Calendar calendar   =   new   GregorianCalendar();
            calendar.setTime(date);
            calendar.add(calendar.DATE,Integer.parseInt(bookList.get(i).get("prod_days").toString()));//把日期往后增加一天.整数往后推,负数往前移动
            date=calendar.getTime();   //这个时间就是日期往后推一天的结果
            SimpleDateFormat formatter1 = new SimpleDateFormat("yyyy-MM-dd ");
            String now = formatter1.format(date);
            bookList.get(i).put("order_date",now);
        }
        view.addObject("cup",true);
        view.addObject("count",bookList.size());
        view.addObject("bookList",bookList);
        return view;
    }

    /**
    * 删除商品
    *
    * */
    @RequestMapping("/deleteBook")
    @ResponseBody
    public CommonResult deleteBook(@RequestParam(value = "bookId") int bookId){
        CommonResult result = new CommonResult();
        result = WxBookService.deleteBook(bookId);
        return result;
    }

    /**
     * 下单
     *
     * */
    @RequestMapping("/insertBook")
    @ResponseBody
    public CommonResult insertBook(@RequestBody String data,HttpServletRequest request){
        CommonResult result = new CommonResult();
        List<Map<String,Object>> sgsList= new ArrayList<Map<String, Object>>();
        String custId =request.getSession().getAttribute("custId").toString();
        Map<String,Object> map = new HashMap<String, Object>();
        map.put("custId",custId);
        JSONArray jsonArray = JSONArray.parseArray(data);
        List<Map<String,Object>> mapListJson = (List)jsonArray;
        List<Map<String,Object>> sgbasCustInfoList = WxBookService.querysgbasCustInfo(custId);
        Date date = new Date();
        int a= Integer.parseInt(WxBookService.selectcompaId());
        for (int i=0 ;i<mapListJson.size();i++){
            Map<String,Object> sgsmap=new HashMap<String, Object>();
            map.put("bookId",mapListJson.get(i).get("bookId"));
            List<Map<String,Object>> bookList= WxBookService.queryByCustId(map);
            List<Map<String,Object>> BasCorpInfoList= WxBookService.querysgBasCorpInfo(mapListJson.get(i).get("purveCode").toString());
             a= a+1;
            String compa_id = ""+a;
            sgsmap.put("compa_id",compa_id);
            sgsmap.put("compa_code",WxBookService.wxCompaCode());
            sgsmap.put("order_date", mapListJson.get(i).get("dateTime"));
            sgsmap.put("consi_id", custId);
            sgsmap.put("consi_name", sgbasCustInfoList.get(0).get("cust_name"));
            sgsmap.put("consi_abbr", sgbasCustInfoList.get(0).get("cust_abbr"));
            sgsmap.put("purve_code", mapListJson.get(i).get("purveCode"));
            sgsmap.put("purve_name", BasCorpInfoList.get(0).get("CORP_NAME"));
            String addr =BasCorpInfoList.get(0).get("NATION_NAME").toString()+BasCorpInfoList.get(0).get("PROVIN_NAME").toString()+BasCorpInfoList.get(0).get("CITY_name").toString()+BasCorpInfoList.get(0).get("ADDR_DETAIL").toString();
            sgsmap.put("addr", addr);
            sgsmap.put("prod_name", bookList.get(0).get("prod_name"));
            sgsmap.put("signup_time", date);
            sgsmap.put("consi_addr", mapListJson.get(i).get("custAddr"));
            sgsmap.put("consi_legalman", sgbasCustInfoList.get(0).get("cust_code"));
            sgsmap.put("bill_sta", 0);
            sgsmap.put("cord_code", mapListJson.get(i).get("cordCode"));
            sgsmap.put("saler_id",sgbasCustInfoList.get(0).get("saler_id"));
            sgsmap.put("saler_code",sgbasCustInfoList.get(0).get("saler_code"));
            sgsmap.put("saler_name",sgbasCustInfoList.get(0).get("saler_name"));
            sgsList.add(sgsmap);
        }

        result = WxBookService.insertsgs(sgsList,mapListJson);
        return result;
    }
}
